package org.xtframe.web.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Repository;
import org.xtframe.bean.SystemConfig;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.ContextUtil;
import org.xtframe.util.JbpmUtil;

@Repository("/deployWorkflowAction")
public class DeployWorkflowAction extends BaseAction {

	private File upload;// 上传文件
	private String uploadFileName;// 上传文件名

	// 上传.zip文件进行部署
	public String deploy() {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try{
			if (ContextUtil.getCurrentUserData() == null) {
				return ajaxJsonErrorMessage("请先登录!");
			}
			if (upload == null) {
				return ajaxJsonErrorMessage("请选择上传部署文件!");
			}
			String allowedUploadDeployExtension = getSystemConfig().getAllowedUploadDeployExtension().toLowerCase();
			if (StringUtils.isEmpty(allowedUploadDeployExtension)){
				return ajaxJsonErrorMessage("不允许上传部署文件!");
			}
			String fileExtension =  StringUtils.substringAfterLast(uploadFileName, ".").toLowerCase();
			String[] fileExtensionArray = allowedUploadDeployExtension.split(SystemConfig.EXTENSION_SEPARATOR);
			if (!ArrayUtils.contains(fileExtensionArray, fileExtension)) {
				return ajaxJsonErrorMessage("只允许上传部署文件类型: " + allowedUploadDeployExtension + "!");
			}
			int uploadLimit = getSystemConfig().getUploadLimit() * 1024;
			if (uploadLimit != 0) {
				if (upload != null && upload.length() > uploadLimit) {
					return ajaxJsonErrorMessage("文件大小超出限制!");
				}
			}
			File uploadFileDir = new File(ServletActionContext.getServletContext().getRealPath(SystemConfig.UPLOAD_DEPLOY_DIR));
			if (!uploadFileDir.exists()) {
				uploadFileDir.mkdirs();
			}
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
			String dateString = simpleDateFormat.format(new Date());
			String uploadFilePath = SystemConfig.UPLOAD_DEPLOY_DIR + dateString + "/" + CommonUtil.getUUID() + "." + fileExtension;
			File file = new File(ServletActionContext.getServletContext().getRealPath(uploadFilePath));
			FileUtils.copyFile(upload, file);
			
			//进行流程部署
			FileInputStream fis = new FileInputStream(file.getAbsolutePath());
			String deploymentId = JbpmUtil.getInstance().newDeployDefinitionByZipStream(new BufferedInputStream(fis));
			
			jsonMap.put(STATUS, SUCCESS);
			jsonMap.put("url", ServletActionContext.getServletContext().getContextPath() + uploadFilePath);
			jsonMap.put("deploy", "发布成功，版本号为:" + deploymentId);
		} catch (Exception e){
			jsonMap.put(STATUS, ERROR);
			e.printStackTrace();
		} finally {
			clear();
		}
		return ajaxJson(jsonMap);
	}
	
	private void clear() {
		upload = null;
		uploadFileName = null;
	}
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

}
